<?php
/*
 * This Class Loads data based on the Zen Cart 1.3.8 SQL Schema into a test Database.
 * There are a minimum of Tables and Entries, to keep the testing environment contolled.
 * Each Insert is executed seperatly (slow, I know), so tables can be commented-out easily
 */

class ZenCartDatabaseWithDummyData {
	private $db;
	
	function __construct() {
		if (TEST_DB_DATABASE != "test") throw new Exception("DummyZenCartDatabase: Be sure to select ONLY a test database for this!");
		$this->db = new Datasource(TEST_DB_SERVER, TEST_DB_DATABASE, TEST_DB_SERVER_USERNAME, TEST_DB_SERVER_PASSWORD);
	}
	
	private function parse_mysql_dump($url){
    $file_content = file($url);
    $query = "";
    foreach($file_content as $sql_line){
      if(trim($sql_line) != "" && strpos($sql_line, "--") === false){
        $query .= $sql_line;
        if(preg_match("/;\s*$/", $sql_line)){
          $result = mysql_query($query)or die(mysql_error());
          $query = "";
        }
      }
    }
  }

	public function createSchema() {
		$this->dropSchema();
		$this->parse_mysql_dump("sql/mysql_zencart_138a.sql");
	}
	
	public function dropSchema() {
		mysql_query("DROP DATABASE IF EXISTS `test`;") or die(mysql_error());
	}
}
?>
